package org.zpmis.domain.user.vo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.zpmis.common.utils.SpringUtil;
import org.zpmis.domain.org.service.SysOrganizationService;
import org.zpmis.domain.user.model.Employee;

import javax.persistence.*;
import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

@Data
public class EmployeeListVo implements Serializable {
    /**
     * 主键id
     */
    private Long id;

    /**
     * 员工编号
     */
    private String number;

    /**
     * 手机号
     */
    private String phone;

    /**
     * 证件类型 1身份证 2护照 3驾驶证
     */
    private Integer cardType;

    /**
     * 证件号码
     */
    private String idCard;

    /**
     * 员工姓名
     */
    private String name;

    /**
     * 性别 0 女 1男 2其他
     */
    private Integer sex;

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 组织部门id
     */
    private Long orgId;

    /**
     * 登录用户表id
     */
    private Long sysUid;

    /**
     * 员工类型 1普通员工 2部门领导 3公司董事
     */
    private Integer type;

    /**
     * 状态 1在职 2离职
     */
    private Integer status;

    /**
     * 创建时间
     */
    private String createTime;

    /**
     * 创建人id
     */
    private Long createId;

    /**
     * 修改时间
     */
    private String updateTime;

    /**
     * 修改人id
     */
    private Long updateId;

    /**
     * 组织部门名称
     */
    private String orgName;

    /**
     * 获取IOC实例
     * @return
     */
    private SysOrganizationService getSysOrganizationService(){
        //获取IOC中的service实例
        return SpringUtil.getBean(SysOrganizationService.class);
    }


    public EmployeeListVo(Employee employee) {
        this.id = employee.getId();
        this.number = employee.getNumber();
        this.phone = employee.getPhone();
        this.cardType = employee.getCardType();
        this.idCard = employee.getIdCard();
        this.name = employee.getName();
        this.sex = employee.getSex();
        this.age = employee.getAge();
        this.orgId = employee.getOrgId();
        this.sysUid = employee.getSysUid();
        this.type = employee.getType();
        this.status = employee.getStatus();
        this.createTime = employee.getCreateTime() == null ? "" : LocalDateTime.ofInstant(Instant.ofEpochMilli(employee.getCreateTime()), ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        this.createId = employee.getCreateId();
        this.updateTime = employee.getUpdateTime() == null ? "" : LocalDateTime.ofInstant(Instant.ofEpochMilli(employee.getUpdateTime()), ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        this.updateId = employee.getUpdateId();
        this.orgName = getSysOrganizationService().findById(employee.getOrgId()) == null ? "" : getSysOrganizationService().findById(employee.getOrgId()).getOrgName();
    }
}